    <?php 
    function singleGenealogy($parent_level, $level = 0, $levelIds)  {
    	?>
    	
    	<?php 
    	if ($parent_level != null){
	       $children = Doctrine_Core::getTable('Level')->getLevelChildren($parent_level->getId());
	       if(count($children) > 0) {
	        	$level++;
		       	foreach ($children as $child) {
		       		$children_count = Doctrine_Core::getTable('Level')->countLevelChildren($parent_level->getId());
		       		$class = $children_count > 0 ? "jstree-open" : "jstree-leaf jstree-last";
		       		?>
		       		<li id="<?php echo $child->getId();?>" class = "<?php echo $class; ?>" >
		       		<a href="#">
		       		<?php $checked = $child->isMemberOfSelection($levelIds) ? "checked = 'checked'" : "" ?>
		       		<?php 
		       		
		             echo '<input '. $checked.' class="ignore" type="checkbox" name="check_'. $child->getId().'" id="check_'. $child->getId().'" />' . '<span class="tree_caption">' . $child->getName() . '</span>';
		             
		             ?>
		             </a>
		             <ul id="ul_<?php echo $child->getId()?>_<?php echo $level?>">
		             <?php 
		             singleGenealogy($child, $level,$levelIds);
		             ?>
		             </ul>
		             </li>
		             <?php 
		       	}
	       }
    	}
       ?>
       <?php 
    }
    
    ?>
    <div class="head"><h5 class="iList">Listing Definition for <?php echo $directory->getName()?></h5></div>
     <form action="<?php echo url_for("listing/getCsv") ?>" method="post" id="valid" class="mainForm" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
         	
     <input type="hidden" value="<?php echo $directory->getId(); ?>" name="listing_directory_id" id="listing_directory_id">
     
    <div style="clear:both">
		<div class="our_subtitle" style="float:left">
			Involved levels
		</div>
		<div class="our_dashed" style="float: left; height: 1px; margin-top: 10px; width: 690px;">
		
		</div>
	</div>
	<div style="clear: both; width: 824px;">
			<?php 
			$i = 0;
			foreach ($categories as $category) {
					$i++;
					$root_level = Doctrine_Core::getTable('Level')->getRootLevel($category->getId());
		       		?>
		       		
		       		<div style="float:left; margin-left: 20px;margin-top: 20px">
				    <div class="head"><h5 class="iList"><?php echo $category->getName()?> Levels Tree</h5></div>
				    <?php if ($root_level != null) {?>
				    <div  id="tree_<?php echo $category->getId()?>" class="demo">
				    	<ul id="ul_<?php echo $category->getId()?>_0">
				    		<li id="<?php echo $root_level->getId();?>" class="jstree-open">
								<?php $checked = $root_level->isMemberOfSelection($levelIds) ? "checked = 'checked'" : "" ?>
								<a href="#"><?php echo '<input type="checkbox" ' . $checked . '  class="ignore"  name="check_'. $root_level->getId().'" id="check_'. $root_level->getId().'" />' .'<span class="tree_caption">' . $root_level->getName(); ?></span></a>
								<ul id="ul_<?php echo $category->getId()?>_1">
						    	<?php 
						    		singleGenealogy($root_level,1,$levelIds);
						    	?>
						    	</ul>
					    	</li>
						</ul>
				    </div>
				    <?php }?>		       		
		       		
		       		
				    <div style="float:right; padding:5px;clear:both;">

				    <input type="checkbox" name="chb_inclusive_<?php echo $category->getId()?>" value="chb_<?php echo $category->getId()?>"> Inclusive?</div>		
				    <input type="hidden" name="categories[]" value="<?php echo  $category->getId() ?>">       		
		       		</div>
     				<?php 
       				if ($i % 3 == 0 ){
       					?>
       					<div style="clear: both;"></div>
       					<?php 
       				}?>
		       		<?php 
		       }?>         		


     </div>
     	
     	<div class="fix"></div>
     	

     	
        <input type="submit" value="Get CSV" class="greyishBtn submitForm" id="save"  />
        
  
     	        
        <div class="fix"></div>
     </form>
     
     
     
<script type="text/javascript">



<?php foreach ($categories as $category) { ?>
    	$("#tree_<?php echo $category->getId()?>")
    		.jstree({ "plugins" : ["themes","html_data","ui"]
        	 })
    		.bind("select_node.jstree", function (event, data) { 
				var li_id = data.rslt.obj.attr("id");
				
				//alert($('#check_' + li_id).attr("checked"));
				$('#check_' + li_id).attr("checked", !$('#check_' + li_id).attr("checked"));
    		})
    		// 2) if not using the UI plugin - the Anchor tags work as expected
    		//    so if the anchor has a HREF attirbute - the page will be changed
    		//    you can actually prevent the default, etc (normal jquery usage)
    		.delegate("a", "click", function (event, data) { event.preventDefault(); });        	 
    	  
<?php }?>

function submitForm(){
    var str=$('form#valid').serialize();

    $.ajax({type: 'POST', url:' <?php echo url_for('listing/'.($form->getObject()->isNew() ? 'create' :'update').
    (!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : ''))?>', script:true, cache:false, data:  str, 
    beforeSend: function(){
        if($("#chb_home_page").attr("checked") == false){
	        if ($("#template_name").attr("value") == "" || $('input:checked', $('form#valid')).length == 0)  return false;
			
        }

		//validate that only a brother is marked
		var parent_arr = new Array();
        $("input:checked:not('#chb_home_page')").each(function()
        		{
    				var chb_id = $(this).attr("id");
    				var temp_arr = chb_id.split("_");
    				var li_id = temp_arr[1];
    				parent_arr.push($('#'+li_id).parent().attr("id"));
    				
        		    // add $(this).val() to your array
        			//parent_arr.push($(this).parent().attr("id"));
        		});

			    var sorted_arr = parent_arr.sort(); // You can define the comparing function here. 
    
				var found = false;
				var index = 0;
				while(!found && index < parent_arr.length){
					if (sorted_arr[index + 1] == sorted_arr[index]){
						found = true;
					}
					index++;
				}
				if (found == true){
					return false;
				}
				
        },

            success: function(data) {
                 $('#template_super_container_div').html(data);
                 $('#template_name').attr('value',"");
               	 
             } });
}

		
		$('form').jqTransform({imgPath:'../images/forms'});

		function loadDirectoryData (){

			var php_url = '<?php echo url_for('listing/loadDirectoryData?directory_id=')?>';
			var directory_id = document.getElementById('listing_directory_id').value;
			php_url = php_url.concat(directory_id);	
			
			$.ajax({type: 'POST', url:php_url, script:true, cache:false, 
				success: function(data) { 
					 $('#template_super_container_div').html(data);
					 $('#templates_name').attr('value',"");
					} });
			
		}


</script>

<style>
	.jqTransformCheckboxWrapper{
	 	
	    margin-top: 0px !important;
	   
	}
	
	.jstree a .jstree-icon { display:none !important; }
	
	 .tree_caption{
	    margin-left: 3px;
	    position: relative;
	    top: -2px;	 	
	 }
</style>
  
